package com.algorithm;


/**
 * 二分查找
 */
public class BinarySearchIi {

    public static int search(int[] nums, int target) {
        int left=0;
        int right=nums.length-1;
        while (left<=right){
            int index=(left+right)/2;
            if (nums[index]==target){
                return index;
            }
            if (target>nums[index]){
                left=index+1;
            }
            if (target<nums[index]){
                right=index-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
//        int[] nums={-1,0,3,5,9,12};
        int[] nums={9};
        int search = search(nums, 9);
        System.out.println(search);
    }

}
